-----------------
-- Core
-----------------

global_delta_time = 1.0 / 60.0
global_width = 256
global_height = 224
global_debug = true

-- drawString color
ds_red = 255
ds_green = 255
ds_blue = 255
ds_alpha = 255

-- global draw string color
function setDrawStringColor(r, g, b, a)
	ds_red = r
	ds_green = g
	ds_blue = b
	ds_alpha = a
end

-- HELPERS FOR ALL :D
-----------------
-- Clamps value
-----------------
function clamp(given, min, max)
    if given < min then
        return min
    elseif given > max then
        return max
    else
        return given
    end
end

-----------------
-- Converts degree to radians
-----------------
function toRadians(degree)
	return degree * (math.pi / 180)
end

-----------------
-- Converts radians to degree
-----------------
function toDegree(radians)
	return radians * (180 / math.pi)
end

-----------------
-- is between
-----------------
function isBetween(value, min, max)
    if value < min then return false end
    if value > max then return false end
    
    return true
end

-----------------
-- Draw rounded text with current font
-----------------
function drawString(text, x, y, limit, align)
	-- needed to fix the height bug
	local font = love.graphics.getFont()
	x = x * 2
	y = y * 2 - font:getHeight()
	love.graphics.push()
	love.graphics.scale(0.5, 0.5)
	--[[
    love.graphics.setColor(60, 60, 60, 255)
    for yf = -2, 2 do
        for xf = -2, 2 do
            if xf ~= 0 or yf ~= 0 then
                if limit ~= nil and align ~= nil then
                    love.graphics.printf(text, x + xf, y + yf, 
                        limit, align)
                else
                    love.graphics.print(text, x + xf, y + yf)
                end
            end
        end
    end
	--]]
	love.graphics.setColor(ds_red, ds_green, ds_blue, ds_alpha)
    if limit ~= nil and align ~= nil then
        love.graphics.printf(text, x, y, limit, align)
    else
        love.graphics.print(text, x, y)
    end
	love.graphics.pop()
end

-- same as above but draws one letter at time
function drawTimedString(text, x, y, current_time, base_time, interval, use_fixed_draw_string)
	if text == nil then dprint("Core", "drawTimedString, given text is nil") return end
	if current_time < base_time then return end

	local font = love.graphics.getFont()
	local rows = 0
	local index = math.floor((current_time - base_time) / interval)

	local char = string.sub(text, 1, index)
	if char == "\n" then rows = rows + 1 end
    
    if use_fixed_draw_string ~= nil then
        if use_fixed_draw_string then
            drawString(char, x, y + (rows * font:getHeight()))
            return
        end
    end
    
	love.graphics.print(char, x, y + (rows * font:getHeight()))
end

-- same as above but draws aligned and wrapped text
-- function writeTextf(text, x, y, limit, align)
--     if text == nil then dprint("Core", "writeTextf, given text is nil") return end
-- 	love.graphics.scale(1.0 / global_scale_x, 1.0 / global_scale_y)
--     love.graphics.printf(text, x * global_scale_x, y * global_scale_y, limit, align)
--     love.graphics.scale(global_scale_x, global_scale_y)
-- end

-----------------
-- Debug print
-----------------
function dprint(class, text)
    if global_debug then
        print(class .. " - " .. text)
    end
end

-----------------
-- XOR
-- x = y = 0/1
-----------------
function bxor(x, y)
    if x == 1 or y == 1 then
        if x == 1 and y == 1 then
            return 0
        else
            return 1
        end
    end
    
    if x == 0 or y == 0 then
        if x == 0 and y == 0 then
            return 0
        else
            return 1
        end
    end
end

-----------------
-- isInt
-----------------
function isInt(value)
    return (value / math.floor(value) == 1)
end

-----------------
-- is negative or positive
-----------------
function isPlusMinus(value)
    if value < 0 then
        return -1
    else
        return 1
    end
end

-----------------
-- Linear interpolation
-- margin is optional
-----------------
function linearInterp(x1, x2, mu, margin)
    local r_value = (x1 * (1.0 - mu) + x2 * mu)
    
    if margin ~= nil then
        local dif = math.abs(x1 - x2)
        if dif < margin then
            return x2
        end
    end
    
    return r_value
end

-----------------
-- Bezier curve
-- p0 = start point
-- p1 = control point
-- p2 = control point
-- p3 = end point
-- step = step (how smooth, more to smoother)
-- Credits: http://www.plus1coins.com/2010/06/drawing-bezier-curves-in-flash/
-----------------
function drawBezierCurve(p0, p1, p2, p3, step)
	local s = 1.0 / step

	local cx = 3 * (p1.x - p0.x)
	local bx = 3 * (p2.x - p1.x) - cx
	local ax = p3.x - p0.x - cx - bx
	
	local cy = 3 * (p1.y - p0.y)
	local by = 3 * (p2.y - p1.y) - cy
	local ay = p3.y - p0.y - cy - by
	
	local x_pos = p0.x
	local y_pos = p0.y
	local prev_x_pos = x_pos
	local prev_y_pos = y_pos
	
	for t = 0, 1, s do
		x_pos = ax * math.pow(t, 3) + bx * math.pow(t, 2) + cx * t + p0.x
		y_pos = ay * math.pow(t, 3) + by * math.pow(t, 2) + cy * t + p0.y
		
		love.graphics.line(prev_x_pos, prev_y_pos, x_pos, y_pos)
		prev_x_pos = x_pos
		prev_y_pos = y_pos
	end
end

-----------------
-- String split
-- PHP-like
-- origin: http://lua-users.org/wiki/SplitJoin
-----------------
function split(str, seperator)
	if str == "" or str == " " then return "" end
	
	local t, ll
	t = {}
	ll = 0
	if #str == 1 then return {str} end
	while true do
		l = string.find(str, seperator, ll, true)
		if l ~= nil then
			table.insert(t, string.sub(str, ll, l - 1))
			ll = l + 1
		else
			table.insert(t, string.sub(str, ll))
			break
		end
	end
	
	return t
end

-----------------
-- random
-----------------
function randomBetween(min, max)
	-- math.randomseed(os.time())
	local random = (math.random() * max * 2) - min
	
	return random
end

-----------------
-- Load new padded ImageData
-----------------
function newPaddedImageData(filename)
	local source = love.image.newImageData(filename)
	local w, h = source:getWidth(), source:getHeight()
	
	local wp = math.pow(2, math.ceil(math.log(w)/math.log(2)))
	local hp = math.pow(2, math.ceil(math.log(h)/math.log(2)))
	
	if wp ~= w or hp ~= h then
		local padded = love.image.newImageData(wp, hp)
		padded:paste(source, 0, 0)
		return padded
	end
	
	return source
end

-- tend to zero function
function tendToZero(value, limit)
    if math.abs(value) < limit then
        return 0.0
    end
    
    return value
end

-- converts int to boolean
function intToBoolean(intvalue)
    if intvalue == 0 then
        return false
    elseif intvalue == 1 then
        return true
    else    
        return false
    end
end

-- draw round rectangle, mode = fill/line
-- function drawRoundRect(mode, x, y, w, h)
--     for i = 1, h - 2 do
--         love.graphics.line(x, y + i, x + w, y + i)
--     end
-- end
